<?php

namespace app\admin\controller;

use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * 站点
 *
 * admin-Site
 *
 * @Apidoc\Title("站点")
 * @Apidoc\Group("api")
 */
class Site extends BaseController
{
    /**
     * 新增
     *
     * admin-Site-add
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\Site::create($data['site']);
            return success();
        } else {
            $options = [];
            $site__member_id_option = \app\common\model\Member::column('name as name, id as id');
            $options['site__member_id_options'] = $site__member_id_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 修改
     *
     * admin-Site-update
     */
    public function update()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\Site::update($data['site']);
            return success();
        } else {
            $data = \app\common\model\Site::find(input('site__id'));
            $r = [];
            foreach ($data->getData() as $key => $val) {
                $r['site__' . $key] = $val;
            }
            View::assign('r', $r);
            $options = [];
            $site__member_id_option = \app\common\model\Member::column('name as name, id as id');
            $options['site__member_id_options'] = $site__member_id_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 删除
     *
     * admin-Site-delete
     */
    public function delete()
    {
        \app\common\model\Site::destroy(input('site__id'));
        return success();
    }
    /**
     * 列表
     *
     * admin-Site-list
     */
    public function list()
    {
        if (request()->isPost()) {
            return error('请求方式错误');
        }
        $query = Db::table('db_site');
        $query->join('db_member', 'db_member.id = db_site.member_id');
        if (input('site__website', '') !== '') {
            $query->where('db_site.website', 'like', '%' . input('site__website', '', 'search_in') . '%');
        }
        if (input('member__id', '') !== '') {
            $query->where('db_member.id', '=', input('member__id', '', 'search_in'));
        }
        $query->where('db_site.delete_time', '=', null);
        $query->where('db_member.delete_time', '=', null);
        $query->field(['db_site.id' => 'site__id']);
        $query->field('db_site.site_demo_id');
        $query->field(['db_site.name' => 'site__name']);
        $query->field(['db_site.create_time' => 'site__create_time']);
        $query->field(['db_site.website' => 'site__website']);
        //$query->field(['db_site.today_quantity' => 'site__today_quantity']);这是之前通过Cron.php发送后来统计的，现在摒弃不用Cron.php了
        $query->field(['db_site.yesterday_quantity' => 'site__yesterday_quantity']);//同上
        $query->field(['db_site.cumulative_quantity' => 'site__cumulative_quantity']);
        $query->field(['db_member.name' => 'member__name']);
        $query->field(['db_member.id' => 'member__id']);
        $query->order('db_site.id', 'desc');
        $data = $query->paginate(input('listRow') ?: 15);
        $data = ['data' => $data->items(), 'paginate' => $data->render()];
        foreach($data['data'] as $key=>$val){
            $data['data'][$key]['system']=Db::name("site_demo")->find($val['site_demo_id'])['name'];
            $data['data'][$key]['site__today_quantity']=Db::name("push_log")->where([
                ["site_id","=",$val['site__id']],
                ["create_time","between",[strtotime(date("Y-m-d 00:00:00")),strtotime(date("Y-m-d 23:59:59"))]],
            ])->count();
            $data['data'][$key]['site__yesterday_quantity']=Db::name("push_log")->where([
                ["site_id","=",$val['site__id']],
                ["create_time","between",[strtotime(date('Y-m-d', strtotime('-1 day')).' 00:00:00'),strtotime(date('Y-m-d', strtotime('-1 day')).' 23:59:59')]],
            ])->count();            

        }
        View::assign($data);
        $options = [];
        $member__id_option = \app\common\model\Member::column('name as name, id as id');
        $options['member__id_options'] = $member__id_option;
        View::assign($options);
        return View::fetch();
    }
}